package net.bxx2004.extand;

import net.bxx2004.extand.commands.MenuOpen;
import net.bxx2004.extand.events.MenuClick;
import net.bxx2004.extand.events.PlayerChat;
import net.bxx2004.extand.events.PlayerJoin;
import net.bxx2004.extand.manager.ConfigManager;
import net.bxx2004.extand.util.JDBCUtil;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;

public class extand extends JavaPlugin {
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(new MenuClick(), this);
        getServer().getPluginManager().registerEvents(new MenuOpen(), this);
        getServer().getPluginManager().registerEvents(new PlayerChat(), this);
        getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
        ConfigManager configManager = new ConfigManager();
        configManager.setUp();
        if (ConfigManager.getSetting().getString("Storge.Type").equalsIgnoreCase("MySql")){
            if (selectTable()){
                System.out.println("[Extand] -> 已经查询到表的存在,不再创建...");
            }else {
                createTable();
            }
        }
        Bukkit.getConsoleSender().sendMessage("§a====================================");
        Bukkit.getConsoleSender().sendMessage("               §bExtand               ");
        Bukkit.getConsoleSender().sendMessage("   §e欢迎使用Extand §f| §dAuthor: bxx2004  ");
        Bukkit.getConsoleSender().sendMessage("§a====================================");
    }

    @Override
    public void onDisable() {

    }
    public boolean selectTable(){
        boolean b;
        try {
            Connection connection = JDBCUtil.getConnection();
            DatabaseMetaData meta = connection.getMetaData();
            ResultSet resultSet = meta.getTables(null, null, ConfigManager.getSetting().getString("Storge.SqlTablename"), new String[]{"TABLE"});
            b = resultSet.next();
            JDBCUtil.close(null, null, connection, null);
            return b;
        }catch (Exception e){}
        return false;
    }
    public void createTable(){
        Connection connection = JDBCUtil.getConnection();
        String sql = "CREATE TABLE "+ ConfigManager.getSetting().getString("Storge.SqlTablename") +" (id INT PRIMARY KEY AUTO_INCREMENT, PlayerName VARCHAR(200), BackPlayer VARCHAR(200), Code VARCHAR(200), Amount INT)";
        if (connection != null){
           try {
               System.out.println("[Extand] -> 数据库链接成功...");
               Statement statement = connection.createStatement();
               statement.executeUpdate(sql);
               System.out.println("[Extand] -> 建表成功...");
               JDBCUtil.close(null, statement, connection, null);
           }catch (Exception e){
               e.printStackTrace();
           }
        }else {
            System.out.println("[Extand] -> 数据库链接失败,请检查配置文件...");
        }
    }
}
